﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PersonBlog.Models;
using PersonBlog.BLL;
using System.Data;
using PersonBlog.Model;
using PersonBlog.Common;

namespace PersonBlog.Controllers
{
    public class LoginController : Controller
    {
        UserBLL userBll = new UserBLL();


        // GET: Login
        public ActionResult Index()
        {
            UserModel user = new UserModel();
            if (DataCache.GetCache("LoginName")!=null)
            {
                user.LoginName = DataCache.GetCache("LoginName").ToString();
            }
            return View(user);
        }

        [HttpPost]
        public ActionResult Index(UserModel user)
        {
            string strWhere = "  LoginName='" + user.LoginName + "' and LoginPwd='" + user.LoginPwd + "'";
            DataSet ds = userBll.GetList(strWhere);
            if (ds.Tables.Count<=0)
            {
                Result r = new Result();
                r.isSuccess = false;
                r.Message = "用户名或密码不正确！";
                ViewBag.Result = r;
                return View(user);
            }
            List<UserModel> userList = DataCache.DataSetToEntityList<UserModel>(ds, 0).ToList();
            if (userList.Count <= 0)
            {
                Result r = new Result();
                r.isSuccess = false;
                r.Message = "用户名或密码不正确！";
                ViewBag.Result = r;
                return View(user);
            }
            else
            {
                //缓存用户名
                DataCache.SetCache("LoginName", userList[0].LoginName);
                //将用户保存到Session中
                Session["CurrentUser"]=userList[0];
                Session.Timeout = 7 * 24 * 60; ;

                return RedirectToAction("Index", "Home");
            }
        }

        [HttpGet]
        public ActionResult Regeister()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Regeister(UserModel user)
        {
            user.CreateTime = DateTime.Now;

            if (userBll.Add(user) <= 0)
            {
                Result r = new Result();
                r.isSuccess = false;
                r.Message = "注册失败！";
                ViewBag.Result = r;
                return View();
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult ForgotPwd()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ForgotPwd(string newpwd)
        {
            if (string.IsNullOrWhiteSpace(newpwd))
            {
                return View();
            }
            UserModel currUser = Session["CurrentUser"] as UserModel;
            bool result = userBll.ModiPwd(newpwd, currUser.ID);
            if (result)
            {
                Session["CurrentUser"] = null;
                return RedirectToAction("Index");
            }
            else
            {
                Result r = new Result();
                r.isSuccess = false;
                r.Message = "密码重置失败！";
                ViewBag.Result = r;
                return View();
            }
           
        }
    }
}